window.onload = function () {
    document.getElementById("administrative").className = "add-nav-active";
    document.getElementById("dispatch-monitoring").className = "add_class";
    document.getElementById("organizational_management_ul").style.display = "inline-block";
    document.getElementById("jiantou").className = "iconfont icon-jiantou-xia";
    station_list();
};



function click_ul() {
    if (document.getElementById("jiantou").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou").className = "iconfont icon-jiantou-xia";
        document.getElementById("organizational_management_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou").className = "iconfont icon-jiantou-shang";
        document.getElementById("organizational_management_ul").style.display = "none";
    }
}

function click_ul1() {
    if (document.getElementById("jiantou1").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou1").className = "iconfont icon-jiantou-xia";
        document.getElementById("gebruikersrol_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou1").className = "iconfont icon-jiantou-shang";
        document.getElementById("gebruikersrol_ul").style.display = "none";
    }
}

function click_ul2() {
    if (document.getElementById("jiantou2").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou2").className = "iconfont icon-jiantou-xia";
        document.getElementById("meeting_management_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou2").className = "iconfont icon-jiantou-shang";
        document.getElementById("meeting_management_ul").style.display = "none";
    }
}

// 加载我的申请列表
function station_list(sent_str, page, countermand, document_id) {
    sent_str = sent_str || "";
    page = page || 1;
    countermand = countermand || false;
    document_id = document_id || '';
    document.getElementById("loading").style.display = "inline-block";
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/administrative/dispatch_monitoring.html", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    if (!countermand) {
        var sendString = sent_str+"&page="+String(page);
    }else {
        sendString = sent_str+"&page="+String(page)+"&recall=True&document_id="+String(document_id);
    }
    console.log(sendString)
    xhr.send(sendString);
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                window.preview_dict = JsonDate["preview_dict"];
                window.receive_dict = JsonDate["receive_dict"];
                create_table(JsonDate["count_num"], JsonDate["data"], page);
                pagenum(JsonDate["count_num"], page);
            }
        }
    };
}

function create_table(count_num, json_data, page) {
    var t_table = document.getElementById("t_table");
    deltr("t_table");
    if (json_data.length === 0){
        document.getElementById("img-display").style.display = "inline-block";
    }else {
        document.getElementById("img-display").style.display = "none";
    }
    for (var i=0; i<json_data.length; i++) {
        var tr = document.createElement("tr");
        var td = document.createElement("td");var td1 = document.createElement("td");var td2 = document.createElement("td");var td3 = document.createElement("td");var td4 = document.createElement("td");var td5 = document.createElement("td");var td6 = document.createElement("td");
        if (json_data[i]["status"] === "进行中" || json_data[i]["status"] === "退回修改"){
            tr.style.color="red";
            if (json_data[i]["views_flag"]){
                td5.innerHTML = "<a onclick='create_process("+json_data[i]["id"]+")' data-toggle=\"modal\" data-target=\".bd-example-modal-xl\" style='color: red;' href='#'><i class=\"iconfont icon-guochengjiankong-lishishuju\"></i>过程</a> | " + "<a style='color: red;' href='/administrative/examine.html?monitoring=true&id="+json_data[i]["id"]+"'><i class=\"iconfont icon-chakanpiyue\"></i>查看</a>" + "<br><a onclick='change_banliren(\""+json_data[i]["task_executor"]+","+json_data[i]["id"]+","+json_data[i]["current_node"]+"\")' data-toggle=\"modal\" data-target=\".bd-example-modal-xl1\" style='color: red;' href='#'><i class=\"iconfont icon-zhuanban\"></i>转办</a>";
            }
            if (json_data[i]["revocation"]){
                td5.innerHTML = "<a onclick='create_process("+json_data[i]["id"]+")' data-toggle=\"modal\" data-target=\".bd-example-modal-xl\" style='color: red;' href='#'><i class=\"iconfont icon-guochengjiankong-lishishuju\"></i>过程</a> | " + "<a style='color: red;' href='/administrative/examine.html?monitoring=true&id="+json_data[i]["id"]+"'><i class=\"iconfont icon-chakanpiyue\"></i>查看</a>" + "<br><a onclick='station_list(sent_str=\"\", page=1, countermand=true, document_id="+json_data[i]["id"]+")' style='color: red;' href='#'><i class=\"iconfont icon-zhuanban\"></i>撤回</a>";
            }
            if (json_data[i]["views_flag"] && json_data[i]["revocation"]){
                td5.innerHTML = "<a onclick='create_process("+json_data[i]["id"]+")' data-toggle=\"modal\" data-target=\".bd-example-modal-xl\" style='color: red;' href='#'><i class=\"iconfont icon-guochengjiankong-lishishuju\"></i>过程</a> | " + "<a style='color: red;' href='/administrative/examine.html?monitoring=true&id="+json_data[i]["id"]+"'><i class=\"iconfont icon-chakanpiyue\"></i>查看</a>" + "<br><a onclick='change_banliren(\""+json_data[i]["task_executor"]+","+json_data[i]["id"]+","+json_data[i]["current_node"]+"\")' data-toggle=\"modal\" data-target=\".bd-example-modal-xl1\" style='color: red;' href='#'><i class=\"iconfont icon-zhuanban\"></i>转办</a> | " + "<a onclick='station_list(sent_str=\"\", page=1, countermand=true, document_id="+json_data[i]["id"]+")' style='color: red;' href='#'><i class=\"iconfont icon-wendangchehui\"></i>撤回</a>";
            }
            if (!json_data[i]["views_flag"] && !json_data[i]["revocation"]) {
                td5.innerHTML = "<a onclick='create_process("+json_data[i]["id"]+")' data-toggle=\"modal\" data-target=\".bd-example-modal-xl\" style='color: red;' href='#'><i class=\"iconfont icon-guochengjiankong-lishishuju\"></i>过程</a> | " + "<a style='color: red;' href='/administrative/examine.html?monitoring=true&id="+json_data[i]["id"]+"'><i class=\"iconfont icon-chakanpiyue\"></i>查看</a>";
            }

            td5.style.textAlign = "center";
        }else {
            td5.innerHTML = "<a onclick='create_process("+json_data[i]["id"]+")' data-toggle=\"modal\" data-target=\".bd-example-modal-xl\" href='#'><i class=\"iconfont icon-guochengjiankong-lishishuju\"></i>过程</a> | "+"<a href='/administrative/examine.html?monitoring=true&id="+json_data[i]["id"]+"'><i class=\"iconfont icon-chakanpiyue\"></i>查看</a>";
            td5.style.textAlign = "center";
        }
        td.innerHTML = json_data[i]["document_num"];td.title=json_data[i]["document_num"];td.style.textAlign = "center";
        td1.innerHTML = json_data[i]["title"];td1.title = json_data[i]["title"];td1.style.width = "8%";td1.style.paddingLeft="0.5%";
        td2.innerHTML = json_data[i]["urgency_degree"];td2.title = json_data[i]["urgency_degree"];td2.style.width = "8%";td2.style.textAlign = "center";
        td3.innerHTML = json_data[i]["task_executor"];td3.title = json_data[i]["task_executor"];td3.style.width = "8%";td3.style.textAlign = "center";
        td4.innerHTML = json_data[i]["status"];td4.title=json_data[i]["status"];td4.style.textAlign = "center";
        td6.innerHTML = json_data[i]["drafter"];td6.title=json_data[i]["drafter"];td6.style.textAlign = "center";
        tr.appendChild(td6);tr.appendChild(td);tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td3);tr.appendChild(td4);tr.appendChild(td5);
        t_table.appendChild(tr);
    }

}


// 撤回公文
function countermand(ele_id) {

}


function create_process(ele_id) {
    deltr("t_table1");deltr("t_table2");
    var t_table = document.getElementById("t_table1");var t_table2 = document.getElementById("t_table2");
    for (var i=0; i<preview_dict[ele_id].length; i++) {
        var tr = document.createElement("tr");
        var td = document.createElement("td");var td1 = document.createElement("td");var td2 = document.createElement("td");var td3 = document.createElement("td");
        td.innerHTML = preview_dict[ele_id][i]["type"];td.title=preview_dict[ele_id][i]["type"];td.style.textAlign = "center";
        td1.innerHTML = preview_dict[ele_id][i]["status"];td1.title = preview_dict[ele_id][i]["status"];td1.style.width = "8%";td1.style.paddingLeft="0.5%";
        td2.innerHTML = preview_dict[ele_id][i]["name"];td2.title = preview_dict[ele_id][i]["name"];td2.style.width = "8%";td2.style.textAlign = "center";
        td3.innerHTML = preview_dict[ele_id][i]["approveTime"];td3.title = preview_dict[ele_id][i]["approveTime"];td3.style.width = "8%";td3.style.textAlign = "center";
        tr.appendChild(td);tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td3);
        t_table.appendChild(tr);
    }

    for (var I=0; I<receive_dict[ele_id].length; I++) {
        console.log("name: ", receive_dict[ele_id][I])
        var tr1 = document.createElement("tr");
        var td_1 = document.createElement("td");var td_2 = document.createElement("td");var td_3 = document.createElement("td");var td_4 = document.createElement("td");
        td_1.innerHTML = receive_dict[ele_id][I]["name"];td_1.title=receive_dict[ele_id][I]["name"];td_1.style.textAlign = "center";
        td_2.innerHTML = receive_dict[ele_id][I]["status"];td_2.title = receive_dict[ele_id][I]["status"];td_2.style.width = "8%";td_2.style.textAlign = "center";
        td_3.innerHTML = receive_dict[ele_id][I]["attention_time"];td_3.title = receive_dict[ele_id][I]["attention_time"];td_3.style.width = "8%";td_3.style.textAlign = "center";
        td_4.innerHTML = "<img style='width: 100px;height: 40px;' src='"+receive_dict[ele_id][I]["signature"]+"'>";td_4.title = receive_dict[ele_id][I]["signature"];td_4.style.width = "8%";td_4.style.textAlign = "center";
        tr1.appendChild(td_1);tr1.appendChild(td_2);tr1.appendChild(td_3);tr1.appendChild(td_4);
        t_table2.appendChild(tr1);
    }
}


function change_banliren(ele_name) {
    document.getElementById("model-affirm2").setAttribute("onclick", "summit_val1('"+ele_name.split(',')[1]+"')")
}

// 删除列表
function deltr(t_table){
    var tb = document.getElementById(t_table);
    var rowNum=tb.rows.length;
    for (i=0;i<rowNum;i++) {
        tb.deleteRow(i);
        rowNum=rowNum-1;
        i=i-1;
    }
}


function pagenum(dataTotals, page) {
    page = page || 1;
    dataTotals = dataTotals || 0;
    new myPagination({
        id: 'pagination',
        curPage: page, //初始页码
        pageAmount: 20,  //每页多少条
        dataTotal: dataTotals, //总共多少条数据
        pageTotal:  Math.ceil(parseInt(dataTotals, 10)/20), //总页数
        pageSize: 5, //可选,分页个数
        showPageTotalFlag:true, //是否显示数据统计
        showSkipInputFlag:true, //是否支持跳转
        getPage: function (page) {
            station_list(sent_str='', page);
            document.getElementById("check_all").checked = false;
        }
    });
}

function click_ul3() {
    if (document.getElementById("jiantou3").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou3").className = "iconfont icon-jiantou-xia";
        document.getElementById("issue_approval_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou3").className = "iconfont icon-jiantou-shang";
        document.getElementById("issue_approval_ul").style.display = "none";
    }
}
